﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DTC.Database;

namespace DTC.Popups
{
    /// <summary>
    /// Interaction logic for PhonePopup.xaml
    /// </summary>
    public partial class PhonePopup : Window
    {
        public PhonePopup()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            phone = new DTCPhone();
            MainGrid.DataContext = phone;
            isNew = true;
        }

        bool isNew;
        private DTCPhone phone;
        public DTCPhone Phone
        {
            get
            {
                return phone;
            }
            set
            {
                phone = value;
                MainGrid.DataContext = phone;
                isNew = false;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.DialogResult == true)
            {
                if (PhoneNumberTB.Text.Length == 0)
                {
                    WarningPopup wp = new WarningPopup();
                    wp.Owner = this;
                    wp.Title = "Недостаточно данных";
                    wp.Message = "Необходимо указать номер телефона";
                    wp.ShowDialog();
                    e.Cancel = true;
                    return;
                }
                bool NeedWrite = false;
                if (phone.Number != PhoneNumberTB.Text)
                {
                    phone.Number = PhoneNumberTB.Text;
                    NeedWrite = true;
                }
                if (phone.Description != DescCB.Text)
                {
                    phone.Description = DescCB.Text;
                    NeedWrite = true;
                }
                if (isNew)
                    phone.Insert();
                else if (NeedWrite)
                    phone.Update();
            }
        }
    }
}
